/**
 * Copyright 2006-2015 handu.com
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.iepacj.mq.rabbit.demo1;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by amssy on 2016/4/12.
 */
public class MessageSender {

    private static String QUEUE_NAME = "queue_demo1";

    private static String EXCHANGE = "first_exchange";

    public static void main(String[] args) throws Exception {

        // 1. ConnectionFactory创建连接
        ConnectionFactory connFact = new ConnectionFactory();
        Connection conn = connFact.newConnection();

        // 2. 创建channel
        final Channel channel = conn.createChannel(10);


        // 3. 声明一个队列
        Map<String, Object> argsMap = new HashMap<>();
        channel.queueDeclare(QUEUE_NAME, true, false, false, argsMap);

        channel.exchangeDeclare(EXCHANGE, "direct");
        channel.queueBind(QUEUE_NAME, EXCHANGE, QUEUE_NAME);

        //channel.basicQos(10);

        final String message = "hello, rabbitmq, this is a demo";

        String routingKey = "rkey.queue.demo1";

        for (int i = 0; i < 100; i++) {
            final int out = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for (int i = 0; i < 100; i++) {
                            String msg = message + ":" + out + ":" + i;
                            Thread.sleep(2000);
                            channel.basicPublish(EXCHANGE, QUEUE_NAME, null, msg.getBytes());
                            System.out.println("发送消息: " + msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        Thread.sleep(Integer.MAX_VALUE);
    }

}




















